package com.gidgabs.modules.api.controller.share;

import com.gidgabs.common.annotation.AuthIgnoreApi;
import com.gidgabs.common.utils.ReturnJson;
import com.gidgabs.common.validator.Assert;
import com.gidgabs.config.GidgabsContants;
import com.gidgabs.common.utils.PaypalUtil;
import com.gidgabs.modules.api.service.journal.JournalJournalService;
import com.gidgabs.modules.api.service.share.PaypalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

/**
 * Created by tby on 2018/7/5.
 */
@Controller
@Api("paypal支付相关")
public class PaypalController {
    @Autowired
    private PaypalUtil paypalUtil;
    @Autowired private PaypalService paypalService;


    @ApiOperation(value = "paypal支付,返回内容为一个url地址,请前端引导跳转到这个地址,以便用户输入账号密码支付")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "money", value = "支付金额", dataType = "double", paramType = "form"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "string", paramType = "header")
    })
    @RequestMapping(value = "pay", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ReturnJson pay(Double money) {
        Assert.isNull(money, "参数不能为空");
        return ReturnJson.success(paypalUtil.pay(money,"TEST"));
    }

    /**
     * paypal回调地址
     * @param PayerID
     * @param paymentId
     * @return
     */
    @RequestMapping(value = GidgabsContants.PAY_PAL_RETURN_URL)
    @AuthIgnoreApi
    @ApiIgnore
    public String callBack(String PayerID, String paymentId) {
        if (paymentId == null || PayerID == null)
            return "redirect:" + GidgabsContants.PAY_PAL_CANCEL_URL;
        boolean success = paypalUtil.executePay(PayerID, paymentId);
        if (success) {
            paypalService.doneBusiness(paymentId);
            return "redirect:" + GidgabsContants.PAY_PAL_SUCCESS_URL;
        } else {
            return "redirect:" + GidgabsContants.PAY_PAL_FAIL_URL;
        }
    }
}
